package com.buss.common.service.impl;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.jweb.core.service.impl.CommonServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.buss.common.util.DownloadFileUtil;




/**
 * 上传文件
 * 
 * @author 荆志宝
 */
@Service
public class FileUploadService extends CommonServiceImpl{

	@Value("${IMAGE_SERVER_URL}")
	private String ImageServerURL;

	public Map uploadFile(MultipartFile uploadFile) {
		// 在此实现图片上传
		try {
			FastDFSClient client = new FastDFSClient("classpath:client.conf");
			// 取图片的原始名称
			String filename = uploadFile.getOriginalFilename();
			// 取扩展名
			String ext = filename.substring(filename.lastIndexOf(".") + 1);
			String url = client.uploadFile(uploadFile.getBytes(), ext);
			// 拼接url
			url = ImageServerURL + url;
			// 返回Map数据
			Map result = new HashMap<>();
			result.put("error", 0);
			result.put("url", url);

			return result;

		} catch (Exception e) {
			e.printStackTrace();
			// 返回Map数据
			Map result = new HashMap<>();
			result.put("error", 1);
			result.put("message", "上传失败");

			return result;
		}
	}
	
	private DownloadFileUtil downloadFileUtil = new DownloadFileUtil();
	private String fileUrl = "";
	private InputStream downLoadFile;
	private String fileName;
	
	public String download(String fileUrl) throws Exception {  
        byte[] bytes = downloadFileUtil.download(fileUrl);
        downLoadFile = new ByteArrayInputStream(bytes);  
        fileName = "fastdfs"+System.currentTimeMillis()+".tar.tz";
        System.out.println(fileName);
        return "downLoad";  
    }

	public DownloadFileUtil getDownloadFileUtil() {
		return downloadFileUtil;
	}

	public void setDownloadFileUtil(DownloadFileUtil downloadFileUtil) {
		this.downloadFileUtil = downloadFileUtil;
	}

	public InputStream getDownLoadFile() {
		return downLoadFile;
	}

	public void setDownLoadFile(InputStream downLoadFile) {
		this.downLoadFile = downLoadFile;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getFileUrl() {
		return fileUrl;
	}

	public void setFileUrl(String fileUrl) {
		this.fileUrl = fileUrl;
	} 
	
}
